import {router} from '../router/index';
import axios from "axios";
import qs from "qs"; // 对参数编码
import Notice from '../../node_modules/iview/src/components/notice';

axios.defaults.timeout = 5000;

const ACCESS_TOKEN = sessionStorage.getItem('access_token');

axios.interceptors.request.use(function (config) { // 请求拦截器
    if (ACCESS_TOKEN) {
        config.headers.Authorization = ACCESS_TOKEN;
    }

    return config;
}, function (err) {
    console.log('axios 请求错误:', err);
    return Promise.reject(err);
});

axios.interceptors.response.use(function(response) {
    if (response.status === 200) {
        if (response.data.code == 1004) {//需要登陆
            console.log('请登录');
            router.push({
                name: 'loginOut'
            });
        }else {//不需要登陆或者已登录
            console.log('Success', response.data);
        }
    }
    return response;
}, function(err){
    if (err.response.status === 401) {
        console.log('请登录');
        router.push({
            name: 'loginOut'
        });
    }
    if (err.response.status === 500) {
        Notice.error({
            title: 'Error Warning',
            desc: `Error：服务器500错误<br>` + err.response.data.message
        });
    }
    return Promise.reject(err);
});

export default {
    /**测试*/
    admin_admin_index : params => {
        return axios.post('admin/admin/index', params);
    },

    /**验证码*/
    admin_admin_captcha : params => {
        return axios.get('admin/admin/captcha', params);
    },

    /***/
    admin_admin_sendSms : params => {
        return axios.post('admin/admin/sendSms', params);
    },

    /**登录*/
    admin_auth_login : params => {
        return axios.post('admin/auth/login', params);
    },

    /**设置token*/
    set_access_token : params => {
        const ACCESS_TOKEN = params;
        axios.interceptors.request.use(function (config) { // 请求拦截器
            if (ACCESS_TOKEN) {
                config.headers.Authorization = ACCESS_TOKEN;
            }

            return config;
        }, function (err) {
            console.log('axios 请求错误:', err);
            return Promise.reject(err);
        });
    },

    /**退出*/
    admin_auth_logout : params => {
        return axios.post('admin/auth/logout', params);
    },

    /**刷新*/
    admin_auth_refresh : params => {
        return axios.post('admin/auth/refresh', params);
    },

    /**注册*/
    admin_auth_register : params => {
        return axios.post('admin/auth/register', params);
    },

    /**添加菜单*/
    admin_menu_add : params => {
        return axios.post('admin/menu/add', params);
    },

    /**编辑菜单*/
    admin_menu_edit : params => {
        return axios.post('admin/menu/edit/'+params.uuid, params);
    },

    /**编辑菜单*/
    admin_menu_remove : params => {
        return axios.post('admin/menu/remove/'+params.uuid);
    },

    /**获取菜单*/
    admin_menu_getList : params => {
        return axios.post('admin/menu/getList', params);
    },

    /**获取用户信息*/
    admin_auth_me : params => {
        return axios.post('admin/auth/me', params);
    },

    /**管理员列表*/
    admin_admin_getList : params => {
        return axios.post('admin/admin/getList', params);
    },

    /**获取角色列表*/
    admin_role_getList : params => {
        return axios.post('admin/role/getList', params);
    },

    /**获取角色详情*/
    admin_role_detail : params => {
        return axios.post('admin/role/detail', params);
    },

    /**新增角色*/
    admin_role_add : params => {
        return axios.post('admin/role/add', params);
    },

    /**编辑角色*/
    admin_role_edit : params => {
        return axios.post('admin/role/edit/'+params.uuid, params);
    },

    /**复制角色*/
    admin_role_copy : params => {
        return axios.post('admin/role/copy/'+params.uuid, params);
    },

    /**删除角色*/
    admin_role_remove : params => {
        return axios.post('admin/role/remove/'+params.uuid);
    },

    /**获取权限列表*/
    admin_permission_getList : params => {
        return axios.post('admin/permission/getList', params);
    },

    /**获取权限详情*/
    admin_permission_detail : params => {
        return axios.post('admin/permission/detail', params);
    },

    /**新增权限*/
    admin_permission_add : params => {
        return axios.post('admin/permission/add', params);
    },

    /**编辑权限*/
    admin_permission_edit : params => {
        return axios.post('admin/permission/edit/'+params.uuid, params);
    },

    /**删除权限*/
    admin_permission_remove : params => {
        return axios.post('admin/permission/remove/'+params.uuid);
    },

    /**附件列表*/
    admin_attache_getList : params => {
        return axios.post('admin/attache/getList', params);
    },

    /**附件下载*/
    admin_attache_download : params => {
        return axios.post('admin/attache/download/'+params.uuid);
    },














    adminAdminGetList : params => {
        return axios.post('/admin/admin/getList', params);
    },




}